import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_rx/src/rx_typedefs/rx_typedefs.dart';

//灰色下边框
BoxDecoration bottom_gary_border = const BoxDecoration(
  border: Border(
    bottom: BorderSide(
      color: Color.fromRGBO(243, 245, 247, 1),
      width: 1.0,
    ),
  ),
);

BoxDecoration top_gary_border = const BoxDecoration(
  border: Border(
    top: BorderSide(
      color: Color.fromRGBO(243, 245, 247, 1),
      width: 1.0,
    ),
  ),
);

/**
   * 没有边框灰色文本的输入框样式
   */
InputDecoration descript(String desc, {Icon? icon = null, double? fontsize = 14, bool? hasPadding = true, EdgeInsets? paddingVallue = const EdgeInsets.symmetric(vertical: 0.0)}) {
  return InputDecoration(icon: icon, hintText: desc, counterText: '', isCollapsed: true, hintStyle: TextStyle(textBaseline: TextBaseline.alphabetic, fontSize: fontsize, fontWeight: FontWeight.w400, color: Color.fromRGBO(153, 153, 153, 1)), contentPadding: hasPadding == true ? paddingVallue : null, border: InputBorder.none);
}

/**
   * 黑色标题字体
   */
TextStyle blackFont(double size) {
  return TextStyle(color: Colors.black, fontSize: size, fontWeight: FontWeight.w500);
}

/**
   * 黑色标题字体
   */
TextStyle whiteFont(double size) {
  return TextStyle(color: Colors.white, fontSize: size, fontWeight: FontWeight.w500);
}

/**
     * 圆角
     */
BoxDecoration border_radis(Color? color, {double? radiusValue = 10.0, Color? borderColor = Colors.transparent}) {
  color ??= Colors.transparent;
  return BoxDecoration(
      borderRadius: BorderRadius.all(Radius.circular(radiusValue!)),
      color: color, //装饰了边框的话，颜色必须写在这里，写在外面会报错
      border: Border.all(color: borderColor!, width: 1));
}

///ios风格输入框样式
BoxDecoration inputStyle() {
  return BoxDecoration(border: Border.all(color: Colors.transparent, style: BorderStyle.none));
}

///无样式文本
TextDecoration textNoDecoration() {
  return TextDecoration.none;
}

///空白边距
Widget emptyMargin(double height) {
  return SizedBox(
    height: height,
  );
}

///空widget
Widget emptyWidget() {
  return const SizedBox(
    width: 0,
    height: 0,
  );
}

///设置文本常规样式(字体,粗细，颜色)
TextStyle setTextStyle(
  double fontSize,
  FontWeight fontWeight,
  Color color, {
  TextDecoration decoration = TextDecoration.none,
}) {
  return TextStyle(fontSize: fontSize, fontWeight: fontWeight, color: color, decoration: decoration, decorationColor: color);
}

///设置商品标签
Widget _setGoodsTag(String tagName, Color bgColor, double radius, EdgeInsetsGeometry padding, style, {EdgeInsetsGeometry margin = const EdgeInsets.all(0), BoxBorder border = const Border.fromBorderSide(BorderSide.none)}) {
  return Container(
      margin: margin,
      decoration: BoxDecoration(border: border, color: bgColor, borderRadius: BorderRadius.all(Radius.circular(radius))),
      child: Padding(
        padding: padding,
        child: Text(
          tagName,
          style: style,
        ),
      ));
}

///商品类型标签(保税、一般贸易)
Widget goodsTypeTag(tagName, {EdgeInsetsGeometry margin = const EdgeInsets.all(0)}) {
  return _setGoodsTag(tagName, Color(0xffFFEEEE), 2, EdgeInsets.only(left: 4.w, right: 4.w, top: 2.h, bottom: 2.h), setTextStyle(11, FontWeight.w400, ThemeConfig.themeColor), margin: margin);
}

///获取商品类型标签
String getGoodsTypeName(int type) {
  switch (type) {
    case 0:
      return '一般贸易';
    case 1:
      return '保税';
    case 2:
      return '海外直邮';
    default:
      return '一般贸易';
  }
}

///商品折扣标签
Widget goodsDiscountTag(tagName, {EdgeInsetsGeometry margin = const EdgeInsets.all(0)}) {
  return _setGoodsTag(
    tagName,
    ThemeConfig.themeColor,
    2,
    EdgeInsets.only(left: 4.w, right: 4.w, top: 2.h, bottom: 2.h),
    setTextStyle(11, FontWeight.w400, Colors.white),
    margin: margin,
  );
}

///商品满减标签
Widget goodsFullReductionTag(tagName, {EdgeInsetsGeometry margin = const EdgeInsets.all(0)}) {
  return _setGoodsTag(tagName, Colors.white, 4, EdgeInsets.only(left: 4.w, right: 4.w, top: 2.h, bottom: 2.h), setTextStyle(11, FontWeight.w400, ThemeConfig.themeColor), margin: margin, border: Border.all(color: ThemeConfig.themeColor));
}

///商品售卖价格 isShowTips 是否显示到手价
Widget goodsSalesPrice(String price, {String tips = '', double fontSize = 15.0, MainAxisAlignment alignment = MainAxisAlignment.start}) {
  return Row(
    mainAxisAlignment: alignment,
    children: [
      Text(
        '￥',
        style: setTextStyle(12, FontWeight.normal, ThemeConfig.themeColor),
      ),
      Container(
        margin: const EdgeInsets.only(left: 0),
        child: Text(
          price,
          style: setTextStyle(fontSize, FontWeight.bold, ThemeConfig.themeColor),
        ),
      ),
      Container(
        margin: const EdgeInsets.only(left: 4),
        child: Text(
          tips,
          style: setTextStyle(10, FontWeight.w400, ThemeConfig.themeColor),
        ),
      )
    ],
  );
}

///商品原价
Widget goodsOriginPrice(String price, {double fontSize = 10.0, TextDecoration decoration = TextDecoration.none}) {
  return price != "null"
      ? Text(
          '￥$price',
          style: setTextStyle(fontSize, FontWeight.w400, ThemeConfig.subTextColor, decoration: decoration),
        )
      : emptyWidget();
}

///实心的主题色按钮
Widget solidButton(String title, Callback onTap, {double fontsize = 14, double width = 339, double height = 40, Color borderColor = Colors.transparent, EdgeInsets margin = EdgeInsets.zero, double radius = 20, Color? backgroundColor, Color? textColor}) {
  return Container(
    margin: margin,
    child: ElevatedButton(
        style: ButtonStyle(
          shadowColor: MaterialStateProperty.all(Colors.transparent),
          elevation: MaterialStateProperty.all(0),
          side: MaterialStateProperty.all(BorderSide(color: borderColor)),
          minimumSize: MaterialStateProperty.all<Size>(Size(width.w, height.h)),
          backgroundColor: MaterialStateProperty.all(backgroundColor ?? ThemeConfig.themeColor),
          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(radius),
            ),
          ),
        ),
        onPressed: onTap,
        child: Text(
          title,
          style: TextStyle(fontSize: fontsize, fontWeight: FontWeight.w500, color: textColor ?? Colors.white),
        )),
  );
}

///商品详情折扣标签
Widget goodsDetailTypeTag(tagName, {EdgeInsetsGeometry margin = const EdgeInsets.all(0)}) {
  return _setGoodsTag(tagName, Color(0xffFFEEEE), 2, EdgeInsets.only(left: 4.w, right: 4.w, top: 2.h, bottom: 2.h), setTextStyle(11, FontWeight.w500, ThemeConfig.themeColor), margin: margin);
}

class BorderedText extends StatelessWidget {
  final String text;
  final TextStyle textStyle;
  final Color borderColor;
  final double borderWidth;
  final double borderRadius;
  final EdgeInsets padding;
  final EdgeInsets margin;

  const BorderedText({
    super.key,
    required this.text,
    this.textStyle = const TextStyle(fontSize: 11, color: Colors.black),
    this.borderColor = Colors.black,
    this.borderWidth = 1.0,
    this.borderRadius = 0.0,
    this.padding = const EdgeInsets.only(left: 4, right: 4, top: 2, bottom: 2),
    this.margin = const EdgeInsets.only(left: 2, right: 2, top: 2, bottom: 0),
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        border: Border.all(color: borderColor, width: borderWidth),
        borderRadius: BorderRadius.circular(borderRadius),
      ),
      child: Text(
        text,
        style: textStyle,
      ),
    );
  }
}

///评价标签
Widget evaluateTag(tagName, {EdgeInsetsGeometry margin = const EdgeInsets.all(0)}) {
  return _setGoodsTag(tagName, Color(0xffFFEEEE), 20, EdgeInsets.only(left: 4.w, right: 4.w, top: 2.h, bottom: 2.h), setTextStyle(11, FontWeight.w500, ThemeConfig.themeColor), margin: margin);
}

FontWeight getFontWeightFromString(String weight) {
  int weightInt = int.tryParse(weight) ?? 400; // 默认值为 400
  switch (weightInt) {
    case 100:
      return FontWeight.w100;
    case 200:
      return FontWeight.w200;
    case 300:
      return FontWeight.w300;
    case 400:
      return FontWeight.w400;
    case 500:
      return FontWeight.w500;
    case 600:
      return FontWeight.w600;
    case 700:
      return FontWeight.w700;
    case 800:
      return FontWeight.w800;
    case 900:
      return FontWeight.w900;
    default:
      return FontWeight.normal; // 默认处理
  }
}
